package com.apes.scm.masterdata.contrast.service;

import com.apes.framework.api.DomainService;
import com.apes.framework.jpa.spec.JpaDsl;
import com.apes.scm.masterdata.contrast.model.ContrastPartyCode;
import com.apes.scm.masterdata.contrast.repository.ContrastPartyCodeRepository;
import com.apes.scm.masterdata.party.model.PartyRole;
import com.apes.scm.masterdata.party.repository.PartyRoleRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service("contrastPartyCodeService")
public class ContrastPartyCodeService extends DomainService {
    @Autowired private ContrastPartyCodeRepository contrastPartyCodeRepository;
    @Autowired private PartyRoleRepository partyRoleRepository;
    public List<PartyRole> gsidGetContrastPartyCode(String pGSID) {
        List<ContrastPartyCode> contrastPartyCodeList = contrastPartyCodeRepository.findAll(JpaDsl.toCriteriaByEq("gsid", pGSID));
        if (contrastPartyCodeList.size() == 0) {
            throw new RuntimeException("同步数据错误，未找到角色对照信息，公司：" + pGSID);
        }
        List<PartyRole> partyRoleList = new ArrayList<>();
        for (ContrastPartyCode contrastPartyCode:contrastPartyCodeList) {
            partyRoleList = partyRoleRepository.findAll(JpaDsl.toCriteriaByEq("party", contrastPartyCode.getParty().getId()));
            if (partyRoleList.size() > 0) {
                break;
            }
        }
        return partyRoleList;
    }
}
